<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>管理平台</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <link rel="stylesheet" href="${request.contextPath}/statics/css/bootstrap.min.css">
    <link rel="stylesheet" href="${request.contextPath}/statics/css/font-awesome.min.css">
    <link rel="stylesheet" href="${request.contextPath}/statics/css/AdminLTE.min.css">
    <!-- AdminLTE Skins. Choose a skin from the css/skins
         folder instead of downloading all of them to reduce the load. -->
    <link rel="stylesheet" href="${request.contextPath}/statics/css/all-skins.min.css">
    <link rel="stylesheet" href="${request.contextPath}/statics/css/main.css">
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body class="hold-transition login-page" style="background: #E0E7E3">
<!--<img src="${request.contextPath}/statics/images/sys/login.jpg" style="max-width: 100%;">-->
<div class="login-box" id="rrapp" v-cloak>
<!--    <div class="login-logo">-->
<!--        <b id="title" style="color: #2b6ea1">管理平台</b>-->
<!--    </div>-->
    <div class="loginPanels">
        <div class="loginLift">
            <img src="${request.contextPath}/statics/images/sys/loginLeft.png">
        </div>
        <!-- /.login-logo -->
        <div class="login-box-body loginRight">
            <p class="login-box-msg">管理员登录</p>
<!--            <div v-if="error" class="alert alert-danger alert-dismissible">-->
<!--                <h4 style="margin-bottom: 0px;"><i class="fa fa-exclamation-triangle"></i> {{errorMsg}}</h4>-->
<!--            </div>-->
            <div v-show="showBody">
                <div class="form-group has-feedback">
                    <input type="text" class="form-control" v-model="username" placeholder="账号">
                    <span class="glyphicon glyphicon-user form-control-feedback"></span>
                </div>
                <div class="form-group has-feedback">
                    <input type="password" class="form-control" v-model="password" placeholder="密码">
                    <span class="glyphicon glyphicon-lock form-control-feedback"></span>
                </div>
                <div class="form-group has-feedback">
                    <input type="text" class="form-control" v-model="captcha" @keyup.enter="login" placeholder="验证码">
                    <span class="glyphicon glyphicon-warning-sign form-control-feedback"></span>
                </div>
                <div class="form-group has-feedback">
                    <img alt="如果看不清楚，请单击图片刷新！" class="pointer" :src="src" @click="refreshCode">
                    &nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:;" @click="refreshCode">点击刷新</a>
                </div>

                <div class="row">
                    <div class="col-xs-5">
                        <button type="button" class="btn btn-primary btn-block btn-flat" @click="clickMe">微信扫码登录
                        </button>
                    </div>
                    <div class="col-xs-3">
                        <div class="checkbox icheck">
                        </div>
                    </div>
                    <!-- /.col -->
                    <div class="col-xs-4">
                        <button type="button" class="btn btn-primary btn-block btn-flat" @click="login">登录</button>
                    </div>
                    <!--          <div class="col-xs-4">-->
                    <!--              <button type="button" class="btn btn-primary btn-block btn-flat" @click="loginNoCode">免密登录</button>-->
                    <!--          </div>-->
                    <!-- /.col -->
                </div>
            </div>

        <div v-show="!showBody">
            <div class="form-group has-feedback" style="text-align: center">
                <img alt="如果失效，请单击图片刷新！" class="pointer" :src="loginQrCode" @click="refreshQrCode">
                <p class="qrcodeHint">用<span style="color: #FE373C;">微信手机版</span>扫描二维码<span @dblclick="clickMe">登</span>录</p>
<!--                <button type="button" class="btn btn-primary btn-block btn-flat" @click="clickMe">返回</button>-->
            </div>
        </div>
        </div>
        <!-- /.social-auth-links -->
    </div>
    <div>
        <p class="provenance">—— 国 内 领 先 的 用 户 运 营 服 务 商 ——</p>
    </div>
    <!-- /.login-box-body -->
</div>
<!-- /.login-box -->
<script src="${request.contextPath}/statics/libs/jquery.min.js"></script>
<script src="${request.contextPath}/statics/libs/vue.min.js"></script>
<script src="${request.contextPath}/statics/libs/bootstrap.min.js"></script>
<script src="${request.contextPath}/statics/libs/jquery.slimscroll.min.js"></script>
<script src="${request.contextPath}/statics/libs/fastclick.min.js"></script>
<script src="${request.contextPath}/statics/libs/app.js"></script>
<script type="text/javascript">

    var vm = new Vue({
        el: '#rrapp',
        data: {
            username: '',
            password: '',
            captcha: '',
            error: false,
            errorMsg: '',
            src: 'captcha.jpg',
            showBody: true,
            loginQrCode: 'loginQrCode.jpg',
            polling: ''
        },
        beforeCreate: function () {
            if (self != top) {
                top.location.href = self.location.href;
            }
        },

        created: function () {
            this.getTitle();
            // this.clickMe();// 先注释，采用密码登录 2022-02-18
        },
        methods: {
            refreshCode: function () {
                this.src = "captcha.jpg?t=" + $.now();
            },
            login: function (event) {
                var data = "username=" + vm.username + "&password=" + vm.password + "&captcha=" + vm.captcha;
                $.ajax({
                    type: "POST",
                    url: "sys/login",
                    data: data,
                    dataType: "json",
                    success: function (result) {
                        if (result.code == 0) {//登录成功
                            parent.location.href = 'index.html';
                        } else {
                            vm.error = true;
                            vm.errorMsg = result.msg;

                            vm.refreshCode();
                        }
                    }
                });
                return false;
            },
            loginNoCode: function (event) {
                var data = "username=" + vm.username;
                $.ajax({
                    type: "POST",
                    url: "sys/loginNoCode",
                    data: data,
                    dataType: "json",
                    success: function (result) {
                        if (result.code == 0) {//登录成功
                            parent.location.href = 'index.html';
                        } else {
                            vm.error = true;
                            vm.errorMsg = result.msg;
                            vm.refreshCode();
                        }
                    }
                });
                return false;
            },
            getTitle: function () {
                var port = location.port;
                var title = "";
                if (port == 8000) {
                    title = '智客推';
                } else if (port == 6001) {
                    title = '智客推v2.0';
                }
                document.title = title + '管理平台';
                $("#title").html(document.title);
            },
            clickMe: function () {
                this.showBody = !this.showBody;

                this.error = false;
                this.errorMsg = '';

                if (this.showBody == true) {
                    clearInterval(this.polling); // 结束轮询
                    this.polling = null;
                } else {
                    this.getDateLoop(); // 开始轮询
                }

                this.refreshQrCode();
            },
            refreshQrCode: function () {
                let port = location.port;
                this.loginQrCode = "loginQrCode.jpg?origin=" + port + "&t=" + $.now();
            },
            getDateLoop: function (timeout = 1000) { // timeout可以写死，也可以动态
                console.log('查询'); // 执行语句
                this.polling = setInterval(() => {
                    this.promiseFun();
                    console.log('查询'); // 轮询中，执行语句
                }, timeout)
            },
            promiseFun: function () {
                var data = "verifyKey=" + vm.verifyKey;
                $.ajax({
                    type: "POST",
                    url: "sys/loginPool",
                    data: data,
                    dataType: "json",
                    success: function (result) {
                        console.log(result);
                        if (result.code == 0) {//登录成功
                            parent.location.href = 'index.html';
                        } else {
                            vm.error = true;
                            vm.errorMsg = result.msg;

                            vm.refreshCode();
                        }
                    }
                });
                return false;
            },
        },
        beforeRouteLeave(to, from, next) {  // 路由跳转前，清除轮询
            next();
            if (this.polling) {
                clearInterval(this.polling);
                this.polling = null;
            }
        },
    });
</script>
</body>
</html>
